<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8"/>
    <title>档案数据导入</title>
    <meta name="renderer" content="webkit"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
    <meta
            name="viewport"
            content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0"
    />
    <link rel="stylesheet" href="../js/layui/css/layui.css" media="all"/>
    <link rel="stylesheet" href="../style/admin.css" media="all"/>
    <link rel="stylesheet" href="../style/home.css" media="all"/>
    <script src="../js/layui/layui.js"></script>
    <style>

    </style>
</head>
<body>
<div class="layui-card " style=" height: 350px; padding: 20px 20px 0 0">
    <form class="layui-form" lay-filter="postData" action="" id="postData">
        <input type="hidden" name="id" value="put" th:value="${data.id}"/>
        <input type="hidden" name="pid" value="put" th:value="${data.pid}"/>
        <input type="hidden" name="tid" value="put" th:value="${data.tid}"/>
        <div class="layui-col-xs6">
            <label class="layui-form-label">接收批次</label>
            <div class="layui-input-block">
                <select name="batchCode" lay-filter="batchCode" lay-verify="required"
                        lay-search="">
                    <option value=""></option>
                    <option
                            th:each="receive : ${archivesReceiveList}"
                            th:value="${receive.batchCode}" th:text="${receive.batchCode}"
                            th:selected="${receive.batchCode eq data.batchCode}">
                    </option>
                </select>
            </div>
        </div>
        <div class="layui-col-xs6">
            <label class="layui-form-label">数据类型</label>
            <div class="layui-input-block">
                <select id="model" name="model" lay-filter="model" lay-verify="required">
                    <option value=""></option>
                    <option value="1" th:selected="${model eq 1}">件</option>
                    <option value="2" th:selected="${model eq 2}">案卷</option>
                    <option value="3" th:selected="${model eq 3}">项目</option>
                </select>
            </div>
        </div>
        <div class="layui-col-xs12">
            <label class="layui-form-label">文件</label>
            <div class="layui-input-block">
                <button type="button" class="layui-btn layui-btn-normal" id="uploadFile">选择文件
                </button>
                <span id="upload-file" style="color: #FF5722;"></span>
            </div>
        </div>

        <button type="submit" class="layui-btn" lay-submit="" lay-filter="postData"
                id="postButton" style="display: none"
        >
            保存
        </button>
    </form>
</div>
<script>
    var pid = '[[${pid}]]';
    var callBackReturn = false;
    var index = parent.layer.getFrameIndex(window.name);
    var file;
    layui.use(['upload', 'form'], function () {
        var $ = layui.$,
            form = layui.form;
        var upload = layui.upload;

        form.render();

        upload.render({
            elem: '#uploadFile'
            , url: '' //此处配置你自己的上传接口即可
            , auto: false
            , accept: "file"
            , exts: "xlsx|xls"
            , choose: function (obj) {
                let files = obj.pushFile();
                obj.preview(function (idx) {
                    file = files[idx]
                    delete files[idx]
                    $('#upload-file').html(file.name)
                })
            }
        });

        form.on('submit(postData)', function (obj) {
            var data = obj.field;
            if (data["file"] === "") {
                parent.layer.msg("请选择需要上传的数据文件", {icon: 0});
                return false;
            }
            delete data.file;
            let fileName = file.name;
            let exts = "xlsx|xls";
            //校验文件格式
            let value = "";
            value = value.length === 0
                ? ((fileName.match(/[^\/\\]+\..+/g) || []) || '')
                : value;

            if (value.length === 0) return;
            if (exts && !RegExp('\\w\\.(' + exts + ')$', 'i').test(escape(value))) {
                layer.msg('选择的文件中包含不支持的格式');
                return false;
            }
            let formDate = new FormData();
            formDate.append("file", file);
            for (let key in data) {
                formDate.append(key, data[key])
            }
            $.ajax({
                url: '../import/ImportArchivesFile', //url
                method: 'POST', //指定HTTP请求类型为POST
                dataType: 'json', //期望返回JSON格式的结果
                processData: false, //不处理发送的数据（必须）
                contentType: false, //不设置Content-Type头信息（必须）
                async: false,
                data: formDate,
                success: function (result) {
                    if (result.code === 0) {
                        parent.layer.msg(result.message, {icon: 1});
                        callBackReturn = true;
                        parent.layer.close(index);
                    } else {
                        parent.layer.alert(result.message, {icon: 2});
                    }
                },
            });
            return false;
        });
    });


    function Submit() {
        document.getElementById('postButton').click();
        return callBackReturn;
    }
</script>
<script type="text/javascript" src="../js/lib/watermark.js"></script>
</body>
</html>